package com.jiayou


import com.jiayou.emnu.FrameType
import com.jiayou.server.ClientSocketServer
import com.jiayou.session.DefaultClientSocketSession
import java.net.InetSocketAddress
import java.util.*
import kotlin.concurrent.thread

/**
@author lishuang
@data   2021-04-23  17:31:31
@info   客户端例子
 */
fun main(args: Array<String>) {
    val clientSocketServer = ClientSocketServer(
        inetSocketAddress = InetSocketAddress("192.168.0.102", 9000),
        clientSocketSession = DefaultClientSocketSession().also {
            it.authenticatePackage = SimpleAuthPackPackageImpl("111111", "123456")
        }
    )
    clientSocketServer.setReceiveHandler {
        println(it)
    }
    clientSocketServer.start(false)
    thread {
        val scanner = Scanner(System.`in`)
        while (scanner.hasNext()) {
            val message = StringBuilder(scanner.nextLine())
//            for (i in 1..5) {
//                message.append(message)
//            }

//            clientSocketServer.sendMessage(   DefaultProtocolImpl(
//                FrameType.MESSAGE,
//                SimpleDataPackageImpl(from = "lishuang", to = "1416360754", message.toString())
//            ))
            clientSocketServer.sendMessage(
                DefaultProtocolImpl(FrameType.BROADCAST, SimpleBroadcastPackageImpl(message.toString()))
            )
        }
    }
}

